//
//  UIDevice+ExNotch.m
//  OC-Example
//
//  Created by lcj on 2020/11/3.
//  Copyright © 2020 apang.men. All rights reserved.
//

#import "UIDevice+ExNotch.h"
#import <sys/sysctl.h>
#import <sys/utsname.h>

@implementation UIDevice (ExNotch)
+ (BOOL)apm_hasNotch {
    static BOOL hasNotch = NO;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // code from https://github.com/react-native-community/react-native-device-info/blob/0808195a3b905ead1350fadb50fbf1b67a26f69f/ios/RNDeviceInfo/RNDeviceInfo.m#L344
        struct utsname systemInfo;
        uname(&systemInfo);
        NSString* deviceId = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
        if ([deviceId isEqualToString:@"i386"] || [deviceId isEqualToString:@"x86_64"] ) {
            deviceId = [NSString stringWithFormat:@"%s", getenv("SIMULATOR_MODEL_IDENTIFIER")];
        }
        if ([[self notchDevicesCode] containsObject:deviceId]) {
            hasNotch = YES;
        }
    });
    return hasNotch;
}

+ (NSArray *)notchDevicesCode {
    //iPhone、iPad、iWatch、Apple TV device list from https://github.com/react-native-device-info/react-native-device-info/blob/88e291422c8047ee5d54e5b60870084650062aa8/ios/RNDeviceInfo/RNDeviceInfo.m#L165
    // notch device list from https://github.com/react-native-community/react-native-device-info/blob/0808195a3b905ead1350fadb50fbf1b67a26f69f/src/internal/devicesWithNotch.ts#L3 <--> https://github.com/react-native-device-info/react-native-device-info/blob/88e291422c8047ee5d54e5b60870084650062aa8/src/internal/devicesWithNotch.ts#L47

    return @[
        @"iPhone10,3", //iPhone X (model A1865, A1902)
        @"iPhone10,6", //iPhone X (model A1901)
        @"iPhone11,2", //iPhone XS (model A2097, A2098)
        @"iPhone11,4", //iPhone XS Max (model A1921, A2103)
        @"iPhone11,6", //iPhone XS Max (model A2104)
        @"iPhone11,8", //iPhone XR (model A1882, A1719, A2105)
        @"iPhone12,1", //iPhone 11
        @"iPhone12,3", //iPhone 11 Pro
        @"iPhone12,5", //iPhone 11 Pro Max
        @"iPhone13,1", //iPhone 12 mini
        @"iPhone13,2", //iPhone 12
        @"iPhone13,3", //iPhone 12 Pro
        @"iPhone13,4"  //iPhone 12 Pro Max
        
        
    ];
}
@end
